package com.example.yls.demoa;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by yls on 2019/5/17.
 */

public class myView extends View {
    private Paint paint=new Paint();
    private float center;
    private float radius;

    private Bitmap bufBitmap;
    private Canvas bufCanvas;
    public myView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint.setColor(Color.RED);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setDither(true);
        starThread();
    }

    private void starThread() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    postInvalidate();
                    try {
                        Thread.sleep(3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        }).start();
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (bufBitmap==null) {
            bufBitmap = Bitmap.createBitmap(right, bottom, Bitmap.Config.ARGB_8888);
        }if (bufCanvas==null) {
            bufCanvas = new Canvas(bufBitmap);
        }
    }
private void drawBuf(){
    bufCanvas.drawColor(Color.WHITE);
    bufCanvas.drawCircle(center,center,50,paint);
    bufCanvas.drawCircle(200,200,radius,paint);

}

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("myView","onDraw");
        canvas.drawBitmap(bufBitmap,0,0,paint);

    }

    public  void setCenter(float center) {
        this.center = center;
        drawBuf();

    }

    public void setRadius(float radius) {
        this.radius = radius;
        drawBuf();
    }
}
